package com.example.weibo_app.controller;


import com.example.weibo_app.common.utils.AjaxResult;
import com.example.weibo_app.domain.Article;
import com.example.weibo_app.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Api(tags = "文章管理")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/addArticle")
    @ApiOperation("添加文章") //ok
    public AjaxResult addArticle(@RequestBody Article article) {
        return AjaxResult.success(articleService.addArticle(article));
    }

    @GetMapping("/Article/{id}")
    @ApiOperation(value = "根据文章ID查询文章") //ok
    public AjaxResult getArticleById(@PathVariable Integer id) {
        return AjaxResult.success(articleService.getArticleById(id));
    }

    @GetMapping("/getArticle")
    @ApiOperation(value = "查询所有文章") //ok
    public AjaxResult getAllArticles() {
        return AjaxResult.success(articleService.getAllArticles());
    }

    @GetMapping("/Article/user/{userId}")
    @ApiOperation(value = "根据用户ID查询文章列表") //ok
    public AjaxResult getArticlesByUserId(@PathVariable Integer userId) {
        return AjaxResult.success(articleService.getArticlesByUserId(userId));
    }


    @PutMapping("/Article/{id}")
    @ApiOperation(value = "更新文章内容") //ok
    public AjaxResult updateArticleContent(@PathVariable Integer id, @RequestBody String content) {
        return AjaxResult.success(articleService.updateArticleContent(id, content));
    }

    @DeleteMapping("/Article/{id}")
    @ApiOperation(value = "删除文章") //ok
    public AjaxResult deleteArticleById(@PathVariable Integer id) {
        return AjaxResult.success(articleService.deleteArticleById(id));
    }
}
